package com.alibaba.cirtus.ots.application.adapter.ability.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.cirtus.ots.application.adapter.ability.model.request.MsgHeaderSDO;
import com.epoch.app.otsapplicationadaptersdk.model.dto.MessageHeaderSDO;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/10/12 16:04
 */
public class RequestUtils {

    public static MessageHeaderSDO generateMessageHeader(String interfacePath,String receiver){
        String messageId = UUID.randomUUID().toString();
        MessageHeaderSDO messageHeader = new MessageHeaderSDO();
        messageHeader.setMessageId(messageId.replaceAll("-", ""));
        messageHeader.setInterfacePath(interfacePath);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messageHeader.setSendTime(dateFormat.format(new Date()));
        messageHeader.setSender("ORZT");
        messageHeader.setReceiver(receiver);
        return messageHeader;
    }

    public static MessageHeaderSDO generateMessageHeader(String interfacePath,String sender,String receiver){
        String messageId = UUID.randomUUID().toString();
        MessageHeaderSDO messageHeader = new MessageHeaderSDO();
        messageHeader.setMessageId(messageId.replaceAll("-", ""));
        messageHeader.setInterfacePath(interfacePath);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messageHeader.setSendTime(dateFormat.format(new Date()));
        messageHeader.setSender(sender);
        messageHeader.setReceiver(receiver);
        return messageHeader;
    }

    public static MsgHeaderSDO generateMessageHeaderCopy(String interfacePath, String sender, String receiver){
        String messageId = UUID.randomUUID().toString();
        MsgHeaderSDO messageHeader = new MsgHeaderSDO();
        messageHeader.setMessageId(messageId.replaceAll("-", ""));
        messageHeader.setInterfaceKey(interfacePath);
        messageHeader.setInterfacePath(interfacePath);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        messageHeader.setSendTime(dateFormat.format(new Date()));
        messageHeader.setSender(sender);
        messageHeader.setReceiver(receiver);
        return messageHeader;
    }

    /**
     * ������Ϣͷ����
     * @param interfacePath �ӿ�·��
     * @param sender ������
     * @param receiver ������
     * @param pattern ���ڸ�ʽģ��
     * @return ����
     */
    public static JSONObject generateMessageHeader(String interfacePath, String sender, String receiver, String pattern){
        String messageId = UUID.randomUUID().toString();
        JSONObject messageHeader = new JSONObject();
        messageHeader.put("messageId",messageId.replaceAll("-", ""));
        messageHeader.put("interface",interfacePath);
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        messageHeader.put("sendTime",dateFormat.format(new Date()));
        messageHeader.put("sender",sender);
        messageHeader.put("receiver",receiver);
        return messageHeader;
    }
}
